package com.dept;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.dept.dao.DeptMapper;
import com.dept.dao.StaffMapper;
import com.dept.entity.DeptEntity;
import com.dept.entity.StaffEntity;

public class staffTest {
	private static SqlSessionFactory sessionFactory;
	private static final String loaction="com/dept/config.xml";
	static{
		try {
			InputStream stream=Resources.getResourceAsStream(loaction);
			sessionFactory=new SqlSessionFactoryBuilder().build(stream);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	public static SqlSessionFactory getSession(){
		return sessionFactory;
	}
	@Test
	public void addDept(){
		SqlSession session=sessionFactory.openSession();
		StaffMapper staffMapper=session.getMapper(StaffMapper.class);
		StaffEntity staff=new StaffEntity();
		staff.setStaffName("staffName");
		staff.setDeptId("deptId");
		try {
			staffMapper.addStaff(staff);
			session.commit();
			System.out.println("id: "+staff.getId());
		} catch (Exception e) {
			System.out.println("提交事务发生异常: "+e.getMessage());
		} finally{
			session.close();
		}
	}
}
